<template>
    <div class="row">
        <div class="col-md-4">
            <div class="form-group">
                <label for="title">标题</label>
                <input type="text" class="form-control" v-model="form.title" placeholder="标题..">
            </div>
            <div class="form-group">
                <label for="summary">摘要</label>
                <el-input
                        type="textarea"
                        placeholder="请输入内容"
                        v-model="form.describe"
                        :rows="4"
                        maxlength="100"
                        show-word-limit>
                </el-input>
            </div>
            <div class="form-group">
                <label for="imageUrl">封面图片</label>
                <el-upload
                        class="avatar-uploader"
                        action="/api/upload"
                        :show-file-list="false"
                        accept="image/gif, image/jpeg, image/png"
                        :on-success="handleImgUrlSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </div>
        </div>

        <div class="col-md-8">
            <div class="form-group">
                <label for="content">文章正文</label>
                <vue-ueditor-wrap ref="ueditor" v-model="form.body" :config="editorOption" @ready="ready" @before-init="addCustomButtom" @beforeInit="addCustomButtom"></vue-ueditor-wrap>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "CourseFormComponent",
        props: ['form'],
        data() {
            return {
                editorOption: {},
            }
        },
        methods:{
            initUpload(){
                this.editorOption =
                    {
                        // 编辑器不自动被内容撑高
                        autoHeightEnabled: false,
                        // 初始容器高度
                        initialFrameHeight:640,
                        // 初始容器宽度
                        initialFrameWidth: '100%',
                        // 上传文件接口（这个地址是我为了方便各位体验文件上传功能搭建的临时接口，请勿在生产环境使用！！！）
                        serverUrl: '/api/upload',
                        // UEditor 资源文件的存放路径，如果你使用的是 vue-cli 生成的项目，通常不需要设置该选项，vue-ueditor-wrap 会自动处理常见的情况，如果需要特殊配置，参考下方的常见问题2
                        UEDITOR_HOME_URL: '/static/UEditor/'
                    };
            },
            handleImgUrlSuccess: function (response) {
                let self = this;
                if (response.code === 200) {
                    self.form.imageUrl = response.url;
                }
            },
            beforeAvatarUpload: function (item) {
                console.log(item)
            },
        },
        mounted(){
            this.initUpload()
        }
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .el-upload__input{
        display: none;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 120px;
        height: 120px;
        line-height: 120px;
        text-align: center;
    }
    .avatar {
        width: 120px;
        height: 120px;
        display: block;
    }
</style>
